//
//  UIImage+Resize.m
//  WhereToEat
//
//  Created by WTFSSD on 2017/6/5.
//  Copyright © 2017年 WTFSSD. All rights reserved.
//

#import "UIImage+Resize.h"

@implementation UIImage (Resize)


-(UIImage *(^)(CGSize))scaleToSize{
    return ^(CGSize dstSize){
        CGImageRef imgRef = self.CGImage;
        CGSize  srcSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
        // not equivalent to
        if (CGSizeEqualToSize(srcSize, dstSize)) {
            return self;
        }
        
        CGFloat scaleRatio = dstSize.width / srcSize.width;
        UIImageOrientation orient = self.imageOrientation;
        CGAffineTransform transform = CGAffineTransformIdentity;
        switch(orient) {
                
            case UIImageOrientationUp: //EXIF = 1
                transform = CGAffineTransformIdentity;
                break;
                
            case UIImageOrientationUpMirrored: //EXIF = 2
                transform = CGAffineTransformMakeTranslation(srcSize.width, 0.0);
                transform = CGAffineTransformScale(transform, -1.0, 1.0);
                break;
                
            case UIImageOrientationDown: //EXIF = 3
                transform = CGAffineTransformMakeTranslation(srcSize.width, srcSize.height);
                transform = CGAffineTransformRotate(transform, M_PI);
                break;
                
            case UIImageOrientationDownMirrored: //EXIF = 4
                transform = CGAffineTransformMakeTranslation(0.0, srcSize.height);
                transform = CGAffineTransformScale(transform, 1.0, -1.0);
                break;
                
            case UIImageOrientationLeftMirrored: //EXIF = 5
                dstSize = CGSizeMake(dstSize.height, dstSize.width);
                transform = CGAffineTransformMakeTranslation(srcSize.height, srcSize.width);
                transform = CGAffineTransformScale(transform, -1.0, 1.0);
                transform = CGAffineTransformRotate(transform, 3.0 * M_PI_2);
                break;
                
            case UIImageOrientationLeft: //EXIF = 6
                dstSize = CGSizeMake(dstSize.height, dstSize.width);
                transform = CGAffineTransformMakeTranslation(0.0, srcSize.width);
                transform = CGAffineTransformRotate(transform, 3.0 * M_PI_2);
                break;
                
            case UIImageOrientationRightMirrored: //EXIF = 7
                dstSize = CGSizeMake(dstSize.height, dstSize.width);
                transform = CGAffineTransformMakeScale(-1.0, 1.0);
                transform = CGAffineTransformRotate(transform, M_PI_2);
                break;
                
            case UIImageOrientationRight: //EXIF = 8
                dstSize = CGSizeMake(dstSize.height, dstSize.width);
                transform = CGAffineTransformMakeTranslation(srcSize.height, 0.0);
                transform = CGAffineTransformRotate(transform, M_PI_2);
                break;
                
            default:
                [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
                
        }
        
        /////////////////////////////////////////////////////////////////////////////
        // The actual resize: draw the image on a new context, applying a transform matrix
        UIGraphicsBeginImageContextWithOptions(dstSize, NO, self.scale);
        
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        if (!context) {
            return [[UIImage alloc] init];
        }
        
        if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
            CGContextScaleCTM(context, -scaleRatio, scaleRatio);
            CGContextTranslateCTM(context, -srcSize.height, 0);
        } else {  
            CGContextScaleCTM(context, scaleRatio, -scaleRatio);
            CGContextTranslateCTM(context, 0, -srcSize.height);
        }
        
        CGContextConcatCTM(context, transform);
        
        // we use srcSize (and not dstSize) as the size to specify is in user space (and we use the CTM to apply a scaleRatio)
        CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, srcSize.width, srcSize.height), imgRef);
        UIImage* resizedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return resizedImage;

    };
}


-(UIImage *(^)(UIImageRenderingMode))setRenderingMode{
    return ^(UIImageRenderingMode mode){
        return [self imageWithRenderingMode:mode];
    };
}


+(instancetype)imageWithColor:(UIColor *)color size:(CGSize)size{
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
    
}

@end
